<?php

namespace app\common\job;

use ShidianUtil\Api\Tencent\Wechat\Wechat;
use think\facade\Log;
use think\queue\Job;

/**
 * 小程序订阅消息
 * Class MiniProgramMessage
 * @package app\common\job
 */
class MiniProgramMessage
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $touser = $data['touser'] ?? '';
        $template_id = $data['template_id'] ?? '';
        $template_data = $data['data'] ?? [];
        $page = $data['page'] ?? '';

        $this->getWechat()->miniProgram(MINIPROGRAM)
            ->subscribeMessage()
            ->send($touser, $template_id, $template_data, $page);

        $job->delete();
    }

    private function getWechat(): Wechat
    {
        return app(Wechat::class);
    }
}
